package com.hyq.learn.api.config.concurrent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author heyuqin
 * @date 2024/7/30 14:17
 */
@Configuration
public class ThreadPoolConfiguration {

    private static final Logger LOGGER = LoggerFactory.getLogger(ThreadPoolConfiguration.class);

    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        String threadNamePrefix = "learn_quest_common_pool_";
        executor.setThreadNamePrefix(threadNamePrefix);
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(50);
        executor.setKeepAliveSeconds(10 * 60);
        executor.setQueueCapacity(200);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 尽量等待执行完成
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        LOGGER.info("learnQuestCommonPool init...");
        return executor;
    }
}
